import os
import sys

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager

#...

# SQLite  URI compatible
WIN = sys.platform.startswith('win')
if WIN:
    profix = 'sqlite:///'
else:
    profix = 'sqlite:////'

app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'


app.config['SQLALCHEMY_DATABASE_URI'] = profix + os.path.join(os.path.dirname(app.root_path), 'data.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

# 实例化 flask_login 扩展类 
login_manager = LoginManager(app)
login_manager.login_view = 'login'
login_manager.login_message = "请先登录!"


@login_manager.user_loader
def load_user(user_id):
    from  watchlist.models import User
    user = User. query.get(int(user_id))
    return user

login_manager.login_view = 'login'

## 模板上下文处理函数
@app.context_processor
# 函数名称可以随意修改
def inject_user(): 
    from watchlist.models import User
    user = User.query.first();
    # 需要返回字典, 等同于 {'user': user}
    return dict(user = user)

## 使用模板继承组织模板 -- 基模板(base template)

from watchlist import views, errors, commands
